package org.example.controllers;

import lombok.extern.slf4j.Slf4j;
import org.example.common.ApiResponse;
import org.example.common.NumberCheck;
import org.example.entitys.systems.Teacher;
import org.example.service.inter.TeacherService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @Description
 * @Author 魏锴
 * @Date 2023/11/24
 */

@Slf4j
@Controller
@ResponseBody
@RequestMapping(value = "/teacher")
public class TeacherController {
    @Autowired
    private TeacherService teacherService;


    @PostMapping("/insert")
    public ApiResponse addTeacher(String name, String subject, String profession, String school, String introduce) {
        Teacher teacher = new Teacher(-1, name, subject, profession, school, introduce, -1);
        log.info("addTeacher( teacher = " + teacher + ")");
        return teacherService.insertTeacher(teacher) ? ApiResponse.ok(teacher) : ApiResponse.failure();

    }

    @PostMapping("/query")
    public ApiResponse queryTeacher(String id) {
        if (NumberCheck.isNotNumber(id)) return ApiResponse.paramIncorrect("id");
        log.info("addTeacher( id = " + id + ")");
        Teacher teacherVO = teacherService.queryTeacherById(Integer.parseInt(id));
        return teacherVO != null ? ApiResponse.ok(teacherVO) : ApiResponse.failure();
    }
}
